package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbCitiesBo;
import org.dromara.system.domain.vo.WbCitiesVo;
import org.dromara.system.domain.WbCities;
import org.dromara.system.mapper.WbCitiesMapper;
import org.dromara.system.service.IWbCitiesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 城市Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-28
 */
@RequiredArgsConstructor
@Service
public class WbCitiesServiceImpl implements IWbCitiesService {

    private final WbCitiesMapper baseMapper;

    /**
     * 查询城市
     */
    @Override
    public WbCitiesVo queryById(Long cityId){
        return baseMapper.selectVoById(cityId);
    }

    /**
     * 查询城市列表
     */
    @Override
    public TableDataInfo<WbCitiesVo> queryPageList(WbCitiesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbCities> lqw = buildQueryWrapper(bo);
        Page<WbCitiesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询城市列表
     */
    @Override
    public List<WbCitiesVo> queryList(WbCitiesBo bo) {
        LambdaQueryWrapper<WbCities> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbCities> buildQueryWrapper(WbCitiesBo bo) {
        LambdaQueryWrapper<WbCities> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCityName()), WbCities::getCityName, bo.getCityName());
        lqw.eq(bo.getProvinceId() != null, WbCities::getProvinceId, bo.getProvinceId());
        return lqw;
    }

    /**
     * 新增城市
     */
    @Override
    public Boolean insertByBo(WbCitiesBo bo) {
        WbCities add = MapstructUtils.convert(bo, WbCities.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCityId(add.getCityId());
        }
        return flag;
    }

    /**
     * 修改城市
     */
    @Override
    public Boolean updateByBo(WbCitiesBo bo) {
        WbCities update = MapstructUtils.convert(bo, WbCities.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbCities entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除城市
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int querySonNum(Collection<Long> provinceIds) {
        return baseMapper.selectSonNum(provinceIds);
    }
}
